import java.util.Scanner;

public class TestWeilai {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        System.out.println(process(x, y, a, b));
    }

    static int ans = Integer.MAX_VALUE;
    public static int process(int x, int y, int a, int b) {
        dfs(a, b, x, y, 0);
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }

    public static void dfs(int cur, int target, int a, int b, int count) {
        if (cur == target) {
            ans = Math.min(ans, count);
            return;
        }

        if (Integer.MAX_VALUE / cur >= a && a * cur <= target && a != 1) {
            dfs(cur * a, target, a, b, count + 1);
        }
        if (Integer.MAX_VALUE / cur >= b && b * cur <= target && b != 1) {
            dfs(cur * b, target, a, b, count + 1);
        }
    }
}
